﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DemoScene : MonoBehaviour
{

    public string DemoName = null;
    public UnitBehaviour[] Units = null;
    public UnitBehaviour PS { get { return Units == null ? null : Units[0]; } }

    // Use this for initialization
    void Start()
    {
        BuildTestScene(DemoName);
        foreach (var u in Units)
            if (u != null)
                u.Init();
    }

    // Update is called once per frame
    void Update()
    {
        PS.Unit.Update(Time.deltaTime, ConnectionType.None);
    }

    void BuildTestScene(string demoName)
    {
        switch (demoName)
        {
            case "1":
                BuildTestScene1();
                break;
            case "2":
                BuildTestScene2();
                break;
            case "3":
                BuildTestScene3();
                break;
        }
    }

    // 电源驱动齿轮，并带动另外大小两个齿轮
    void BuildTestScene1()
    {
        var ps = new PowerSource();
        ps.AngularVelocity = -Mathf.PI * 0.5f;
        PS.Unit = ps;

        var g1 = new Gear();
        g1.Radius = 1;
        Units[1].Unit = g1;
        g1.Pos = new Vector2(Units[1].transform.position.x, Units[1].transform.position.y);
        ps.LinkChild(g1, ConnectionType.Concentric);

        var g2 = new Gear();
        g2.Radius = 1;
        Units[2].Unit = g2;
        g2.Pos = new Vector2(Units[2].transform.position.x, Units[2].transform.position.y);
        g1.LinkChild(g2, ConnectionType.Clenching);

        var g3 = new Gear();
        g3.Radius = 4;
        Units[3].Unit = g3;
        g3.Pos = new Vector2(Units[3].transform.position.x, Units[3].transform.position.y);
        g2.LinkChild(g3, ConnectionType.Clenching);
    }

    // 电源驱动凸轮，并带动一个步进齿轮
    void BuildTestScene2()
    {
        var ps = new PowerSource();
        ps.AngularVelocity = -Mathf.PI;
        PS.Unit = ps;

        var g1 = new Gear();
        g1.Convex = true;
        g1.Radius = 1;
        Units[1].Unit = g1;
        g1.Pos = new Vector2(Units[1].transform.position.x, Units[1].transform.position.y);
        ps.LinkChild(g1, ConnectionType.Concentric);

        var g2 = new Gear();
        g2.Convex = true;
        g2.Radius = 0.5f;
        Units[2].Unit = g2;
        g2.Pos = new Vector2(Units[2].transform.position.x, Units[2].transform.position.y);
        g1.LinkChild(g2, ConnectionType.Clenching);

        var g3 = new Gear();
        g3.Convex = true;
        g3.Radius = 1;
        Units[3].Unit = g3;
        g3.Pos = new Vector2(Units[3].transform.position.x, Units[3].transform.position.y);
        g1.LinkChild(g3, ConnectionType.Clenching);

        var g4 = new Gear();
        g4.Radius = 1;
        Units[4].Unit = g4;
        g4.Pos = new Vector2(Units[4].transform.position.x, Units[4].transform.position.y);
        g1.LinkChild(g4, ConnectionType.Clenching);

        var g5 = new Gear();
        g5.Radius = 1;
        Units[5].Unit = g5;
        g5.Pos = new Vector2(Units[5].transform.position.x, Units[5].transform.position.y);
        g1.LinkChild(g5, ConnectionType.Clenching);
    }

    // 电源驱动凸轮，并带动连杆
    void BuildTestScene3()
    {
        var ps = new PowerSource();
        ps.AngularVelocity = -Mathf.PI;
        PS.Unit = ps;

        var g1 = new Gear();
        g1.Convex = true;
        g1.Radius = 1;
        g1.ToothStartAngle = Mathf.PI / 6;
        g1.ToothEndAngle = Mathf.PI * 5 / 6;
        Units[1].Unit = g1;
        g1.Pos = new Vector2(Units[1].transform.position.x, Units[1].transform.position.y);
        ps.LinkChild(g1, ConnectionType.Concentric);

        var g2 = new Gear();
        g2.Convex = true;
        g2.Radius = 1;
        Units[2].Unit = g2;
        g2.Pos = new Vector2(Units[2].transform.position.x, Units[2].transform.position.y);
        g1.LinkChild(g2, ConnectionType.Clenching);

        var g3 = new Stick();
        g3.Length = 5;
        Units[3].Unit = g3;
        g3.Pos = new Vector2(Units[3].transform.position.x, Units[3].transform.position.y);
        g2.LinkChild(g3, ConnectionType.Clenching);

        var g4 = new Stick();
        g4.Length = 2;
        Units[4].Unit = g4;
        g4.Pos = new Vector2(Units[4].transform.position.x, Units[4].transform.position.y);
        g1.LinkChild(g4, ConnectionType.Clenching);

        var g5 = new Stick();
        g5.Length = 2;
        Units[5].Unit = g5;
        g5.Pos = new Vector2(Units[5].transform.position.x, Units[5].transform.position.y);
        g1.LinkChild(g5, ConnectionType.Clenching);

        var g6 = new Stick();
        g6.Length = 2;
        Units[6].Unit = g6;
        g6.Pos = new Vector2(Units[6].transform.position.x, Units[6].transform.position.y);
        g3.LinkChild(g6, ConnectionType.Clenching);

        var g7 = new Stick();
        g7.Length = 1;
        Units[7].Unit = g7;
        g7.Pos = new Vector2(Units[7].transform.position.x, Units[7].transform.position.y);
        g2.LinkChild(g7, ConnectionType.Concentric);
    }
}
